﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CompilationFactures.Enums;
using CompilationFactures.Objects;

namespace CompilationFactures.Controls
{
    public partial class ucFacture : UserControl
    {
        Facture factureCalcul;
        List<Facture> listeFactureCalcul;

        public ucFacture()
        {
            InitializeComponent();
            cmbTypeFacture.DataSource = Enum.GetNames(typeof(EnumTypeFacture));
            listeFactureCalcul = new List<Facture>();
        }

        private void cmbTypeFacture_SelectedIndexChanged(object sender, EventArgs e)
        {
            ChargerListeFacture();

            ResetListes();
        }

        private void btnCreerFacture_Click(object sender, EventArgs e)
        {
            ResetListes();

            try
            {
                Facture nouvelleFacture;
                if (cmbTypeFacture.SelectedItem.ToString().Equals(EnumTypeFacture.Garage.ToString()))
                {
                    nouvelleFacture = new FactureGarage();
                }
                else if (cmbTypeFacture.SelectedItem.ToString().Equals(EnumTypeFacture.Epicerie.ToString()))
                {
                    nouvelleFacture = new FactureEpicerie();
                }
                else
                {
                    throw new Exception("Un problème est survenue lors de la création d'une facture... Aucun type n'est sélectionné.");
                }

                DictionnaireDonnees.ListeFacture.Add(nouvelleFacture);
                (this.Parent as FormulairePrincipale).AfficherArticle(true, nouvelleFacture);
                ChargerListeFacture();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ChargerListeFacture()
        {
            List<Facture> listeFacture = new List<Facture>();
            foreach (Facture facture in DictionnaireDonnees.ListeFacture)
            {
                if (cmbTypeFacture.SelectedItem.ToString().Equals(EnumTypeFacture.Epicerie.ToString()))
                {
                    if (facture is FactureEpicerie)
                    {
                        listeFacture.Add(facture);
                    }
                }
                else
                {
                    if (facture is FactureGarage)
                    {
                        listeFacture.Add(facture);
                    }
                }
            }
            lstListeFacture.DataSource = listeFacture;
        }

        private void lstListeFacture_DoubleClick(object sender, EventArgs e)
        {
            Facture FactureCourante = (Facture)lstListeFacture.SelectedItem;
            (this.Parent as FormulairePrincipale).AfficherArticle(true, FactureCourante);
        }

        private void btnSupprimer_Click(object sender, EventArgs e)
        {
            foreach (var item in lstListeFacture.SelectedItems)
            {
                DictionnaireDonnees.ListeFacture.Remove((Facture)item);
            }
            ChargerListeFacture();
        }

        private void btnAddition_Click(object sender, EventArgs e)
        {
            foreach (var item in lstListeFacture.SelectedItems)
            {
                Facture itemFacture = (Facture)item;
                itemFacture.Operation = Operation.Addition;
                listeFactureCalcul.Add(itemFacture.Copier());

                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = "+";
                lvItem.SubItems.Add(itemFacture.NoFacture.ToString());
                lvItem.SubItems.Add(itemFacture.SousTotal.ToString());
                lvItem.SubItems.Add(itemFacture.Total.ToString());
                if (cmbTypeFacture.SelectedItem.ToString().Equals(EnumTypeFacture.Epicerie.ToString()))
                {
                    if (factureCalcul == null)
                    {
                        factureCalcul = itemFacture.Copier();
                    }
                    else
                    {
                        factureCalcul = (FactureEpicerie)factureCalcul + (FactureEpicerie)itemFacture;
                    }
                }
                else
                {
                    if (factureCalcul == null)
                    {
                        factureCalcul = itemFacture.Copier();
                    }
                    else
                    {
                        factureCalcul = (FactureGarage)factureCalcul + (FactureGarage)itemFacture;
                    }
                }
                lvCalculFactures.Items.Add(lvItem);
            }

            CalculerTotaux();
        }

        private void btnSoustraction_Click(object sender, EventArgs e)
        {
            foreach (var item in lstListeFacture.SelectedItems)
            {
                Facture itemFacture = (Facture)item;
                itemFacture.Operation = Operation.Soustraction;
                listeFactureCalcul.Add(itemFacture.Copier());

                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = "-";
                lvItem.SubItems.Add(itemFacture.NoFacture.ToString());
                lvItem.SubItems.Add(itemFacture.SousTotal.ToString());
                lvItem.SubItems.Add(itemFacture.Total.ToString());
                if (cmbTypeFacture.SelectedItem.ToString().Equals(EnumTypeFacture.Epicerie.ToString()))
                {
                    if (factureCalcul == null)
                    {
                        factureCalcul = itemFacture.Copier();
                        factureCalcul.ToNegative();
                    }
                    else
                    {
                        factureCalcul = (FactureEpicerie)factureCalcul - (FactureEpicerie)itemFacture;
                    }
                }
                else
                {
                    if (factureCalcul == null)
                    {
                        factureCalcul = itemFacture.Copier();
                        factureCalcul.ToNegative();
                    }
                    else
                    {
                        factureCalcul = (FactureGarage)factureCalcul - (FactureGarage)itemFacture;
                    }
                }
                lvCalculFactures.Items.Add(lvItem);
            }

            CalculerTotaux();
        }
        

        private void CalculerTotaux()
        {
            if (factureCalcul != null)
            {
                lblMontantSousTotal.Text = string.Format("{0:0.00} $", factureCalcul.SousTotal);
                lblMontantTPS.Text = string.Format("{0:0.00} $", factureCalcul.TPS);
                lblMontantTVQ.Text = string.Format("{0:0.00} $", factureCalcul.TVQ);
                lblMontantTotal.Text = string.Format("{0:0.00} $", factureCalcul.Total);
            }
        }

        private void ResetTotaux()
        {
            lblMontantSousTotal.Text = string.Format("{0:0.00} $", "0");
            lblMontantTPS.Text = string.Format("{0:0.00} $", "0");
            lblMontantTVQ.Text = string.Format("{0:0.00} $", "0");
            lblMontantTotal.Text = string.Format("{0:0.00} $", "0");
        }

        private void ResetListes()
        {
            factureCalcul = null;
            if (listeFactureCalcul != null)
                listeFactureCalcul.Clear();

            lvCalculFactures.Items.Clear();

            ResetTotaux();
        }

        private void btnAjouter_Click(object sender, EventArgs e)
        {
            if (factureCalcul != null)
            {
                factureCalcul.AttribuerNumero();
                DictionnaireDonnees.ListeFacture.Add(factureCalcul);
                ChargerListeFacture();
                factureCalcul = null;
                lvCalculFactures.Items.Clear();
                ResetTotaux();
            }
        }

        public Facture GetFactureSelected()
        {
            if (lstListeFacture.SelectedItems.Count != 1)
            {
                MessageBox.Show("Il n'y a aucune facture sélectionnée.");
                return null;
            }
            else
            {
                return (Facture)lstListeFacture.SelectedItem;
            }
        }

        public void UpdateListeFactures()
        {
            if (this.Visible)
                ChargerListeFacture();

        }
        private void ucFacture_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
                ChargerListeFacture();
        }

        private void btnRetrait_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lvCalculFactures.SelectedItems)
            {
                string operation = "";

                switch (item.Text)
                {
                    case "-": operation = "Soustraction";
                        break;

                    case "+": operation = "Addition";
                        break;

                    default: operation = "Aucun";
                        break;
                }
                Facture factureCourante = (Facture)listeFactureCalcul.Find( fact => fact.NoFacture.ToString().Equals(item.SubItems[1].Text) 
                                                                                    && fact.Operation.ToString().Equals(operation)
                                                                                    && fact.Total.ToString().Equals(item.SubItems[3].Text)
                                                                          );

                if (cmbTypeFacture.SelectedItem.ToString().Equals(EnumTypeFacture.Epicerie.ToString()))
                {
                    if (factureCourante.Operation.Equals(Operation.Addition))
                    {
                        factureCalcul = (FactureEpicerie)factureCalcul - (FactureEpicerie)factureCourante;
                    }
                    else
                    {
                        factureCalcul = (FactureEpicerie)factureCalcul + (FactureEpicerie)factureCourante;
                    }
                }
                else
                {
                    if (factureCourante.Operation.Equals(Operation.Addition))
                    {
                        factureCalcul = (FactureGarage)factureCalcul - (FactureGarage)factureCourante;
                    }
                    else
                    {
                        factureCalcul = (FactureGarage)factureCalcul + (FactureGarage)factureCourante;
                    }
                }

                listeFactureCalcul.Remove(factureCourante);
                lvCalculFactures.Items.Remove(item);
            }

            CalculerTotaux();
        }
    }
}
